﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace LightWadf.Core.Route
{
    public class RouteItem
    {
        public string stringValue;

        private string preString = null;
        private string sufString = null;
        private string attributeName = null;

        private IRouteParameter parameter = null;

        /// <summary>
        /// 根据路由规则解析 item 所代表的含义
        /// </summary>
        /// <param name="item"></param>
        public void ParseItem(string item)
        {
            this.stringValue = item;

            Regex regex = new Regex(@"([^{]*)(?:{([^\:]*)(?:\:t\[([^\]]*)\])?([^}]*)})?(.*)");
            Match match = regex.Match(item);
            this.preString = match.Groups[1].Value;
            this.sufString = match.Groups[5].Value;
            this.attributeName = match.Groups[2].Value;
            string typeName = match.Groups[3].Value;
            if (!string.IsNullOrEmpty(typeName))
            {
                switch (typeName)
                {
                    case "string":
                        if (!string.IsNullOrEmpty(match.Groups[4].Value))
                            this.parameter = new StringRouteParameter(match.Groups[4].Value);
                        else this.parameter = new StringRouteParameter();
                        break;
                    case "int":
                        if (!string.IsNullOrEmpty(match.Groups[4].Value))
                            this.parameter = new IntegerRouteParameter(match.Groups[4].Value);
                        else this.parameter = new IntegerRouteParameter();
                        break;
                    case "double":
                        if (!string.IsNullOrEmpty(match.Groups[4].Value))
                            this.parameter = new DoubleRouteParameter(match.Groups[4].Value);
                        else this.parameter = new DoubleRouteParameter();
                        break;
                    case "decimal":
                        if (!string.IsNullOrEmpty(match.Groups[4].Value))
                            this.parameter = new DecimalRouteParameter(match.Groups[4].Value);
                        else this.parameter = new DecimalRouteParameter();
                        break;
                    default:
                        if (!string.IsNullOrEmpty(match.Groups[4].Value))
                            this.parameter = new StringRouteParameter(match.Groups[4].Value);
                        break;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(match.Groups[4].Value))
                    this.parameter = new StringRouteParameter(match.Groups[4].Value);
            }
        }

        /// <summary>
        /// 判断 item 是否符合当前 Item 的规则
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Match(string item, Dictionary<string, object> param)
        {
            if (string.IsNullOrEmpty(item)) return false;

            // 检测前缀
            if (!string.IsNullOrEmpty(this.preString))
            {
                if (item.IndexOf(this.preString) != 0)
                {
                    return false;
                }
            }

            // 检测后缀
            if (!string.IsNullOrEmpty(this.sufString))
            {
                if (item.IndexOf(this.sufString) != item.Length - this.sufString.Length)
                {
                    return false;
                }
            }

            // 检测中间部分
            string mid = item.Substring(this.preString.Length, item.Length - this.sufString.Length - this.preString.Length);
            if (!string.IsNullOrEmpty(mid) && this.parameter != null)
            {
                var result = this.parameter.Match(mid);
                if (result.Item1)
                {
                    param.Add(this.attributeName, result.Item2);
                }
                return this.parameter.Match(item).Item1;
            }
            else if (string.IsNullOrEmpty(mid) && this.parameter == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断路由规则是否一致
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Equal(string item){
            return this.stringValue.Equals(item);
        }

        public bool Equal(RouteItem item){
            return this.stringValue.Equals(item.stringValue);
        }
    }
}